package com.dream.xcarrecorder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
import com.dream.xcarrecorder.test.AudioPlayRateTester;
import com.dream.xcarrecorder.test.MP3Tester;
import java.io.File;
import java.io.IOException;

public class AudioActivity extends Activity implements View.OnClickListener{

    private Context mContext;
    private Button mBtnRec//录制
            ,mBtnPlayNormal//正常播放
            ,mBtnPlayTempo//通过改变节奏二倍速播放（听感上二倍速，实际时长不一定减半，音调正常）
            ,mBtnPlayRate;//通过改变速率二倍速播放（时长减半，但音调会变）
    private final MediaRecorder mMediaRec = new MediaRecorder();
    private String mAudioPath = "";
    private boolean isRecording = false;

    private final MP3Tester mMp3Tester = new MP3Tester();//MP3编码录制测试类
    private final AudioPlayRateTester mAudioPlayTester =
            new AudioPlayRateTester();//音频解码变速播放测试类


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio);
        mContext = this;
        mAudioPath =
                getExternalFilesDir(Environment.DIRECTORY_PICTURES)+ File.separator+"recorder"+ SystemClock.uptimeMillis()+".mp3";

        mBtnRec = findViewById(R.id.audio_btn_record);
        mBtnRec.setOnClickListener(this);
        mBtnPlayNormal = findViewById(R.id.audio_btn_play_normal);
        mBtnPlayNormal.setOnClickListener(this);
        mBtnPlayTempo = findViewById(R.id.audio_btn_play_tempo);
        mBtnPlayTempo.setOnClickListener(this);
        mBtnPlayRate = findViewById(R.id.audio_btn_play_rate);
        mBtnPlayRate.setOnClickListener(this);
        findViewById(R.id.audio_btn_mp3).setOnClickListener(this);
        findViewById(R.id.audio_btn_mp3_play).setOnClickListener(this);

        mMediaRec.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mMediaRec.setOutputFile(mAudioPath);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.audio_btn_record){
            if (isRecording){
                mMediaRec.stop();
                mMediaRec.release();
                isRecording = false;
                mBtnRec.setText("开始录制");

                mBtnPlayNormal.setEnabled(true);
                mBtnPlayRate.setEnabled(true);
                mBtnPlayTempo.setEnabled(true);
            }else{
                try {
                    mMediaRec.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mMediaRec.start();
                isRecording = true;
                mBtnRec.setText("停止录制");
            }
            //TODO:录制
        }else if (id == R.id.audio_btn_play_normal){
            //TODO:正常播放
            mAudioPlayTester.setRate(1);
            mAudioPlayTester.play(mAudioPath);
        }else if (id == R.id.audio_btn_play_tempo) {
            //TODO:通过改变节奏二倍速播放（听感上二倍速，实际时长不一定减半，音调正常）
            mAudioPlayTester.setTempo(2);
            mAudioPlayTester.play(mAudioPath);
        }else if (id == R.id.audio_btn_play_rate) {
            //TODO:通过改变速率二倍速播放（时长减半，但音调会变）
            mAudioPlayTester.setRate(2);
            mAudioPlayTester.play(mAudioPath);
        }else if (id == R.id.audio_btn_mp3){
            //开始录制MP3
            mMp3Tester.startRec(mContext);
        }else if (id == R.id.audio_btn_mp3_play){
            //停止录制MP3
            mMp3Tester.stopRec();
            //播放MP3
            mMp3Tester.play();
        }
    }

    public static void startActivity(Context ct){
        Intent i = new Intent(ct, AudioActivity.class);
        ct.startActivity(i);
    }
}
